package com.curry.apiprovider.util;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;
/**
 * 
 * @author Zale
 *
 */
public class IDGenerator {

	private static long __previous_time__ = 0L;
	private static int __machine_key__ = 0;
	private static int __sequence_no__ = 0;
	
	/*
	 * 生成唯一向前滚动的序列号。 根据IP在不同机器上保持唯一性。 最大性能为10ms产生最多10个不重复序列号
	 */
	public synchronized static long generateUUID() {
		if (__machine_key__ == 0L) {
			// 获取本机IP地址
			int[] args = Convert.ipv4ToIntArray(Machine.getIPAddress());
			if (args == null)
				args = new int[] { (int) (Math.random() * 255),
						(int) (Math.random() * 255),
						(int) (Math.random() * 255),
						(int) (Math.random() * 255) };
			__machine_key__ = args[0] + args[1] + args[2] + args[3];
			if (__machine_key__ > 999)
				__machine_key__ -= 1000;
			__machine_key__ = (__machine_key__ * 1000)
					+ ((int) (Math.random() * 9) * 100)
					+ ((int) (Math.random() * 9) * 10);
		}
			
		long now = System.currentTimeMillis();
		if (now == __previous_time__) {
			__sequence_no__++;
		} else {
			__previous_time__ = now;
			__sequence_no__ = 0;
		}
		long uuid =  __previous_time__ * 1000 + __machine_key__ + __sequence_no__;
		return uuid;
	}

	/*
	 * 生成验证码
	 * 
	 * @param count 验证码的位数
	 * 
	 * @param hasCapitalLetter 是否有大写
	 * 
	 * @param hasLowercase 是否有小写
	 * 
	 * @param hasDigit 是否有数字
	 * 
	 * @param hasInterpunction 是否有标点符号
	 * 
	 * @return 验证码，例：7kFlr9#
	 */
	public static String generateCode(int count, boolean hasCapitalLetter,
			boolean hasLowercase, boolean hasDigit, boolean hasInterpunction) {
		String result = "";
		if (count < 1)
			return result;

		String capitalLetter = "ABCDEFGHJKLMNPQRSTUVWXY";
		String lowercase = "abcdefghjklmnpqrstuvwxy";
		String digit = "23456789";
		String interpunction = "~!@#$%^&*()_+=-{}|][:;?/.";

		List<Boolean> matchs = new ArrayList<Boolean>();
		List<String> condis = new ArrayList<String>();
		if (hasCapitalLetter) {
			condis.add(capitalLetter);
			matchs.add(false);
		}
		if (hasLowercase) {
			condis.add(lowercase);
			matchs.add(false);
		}
		if (hasDigit) {
			condis.add(digit);
			matchs.add(false);
		}
		if (hasInterpunction) {
			condis.add(interpunction);
			matchs.add(false);
		}

		int conditionCount = condis.size();
		int unmatchedCount = conditionCount;
		if (conditionCount == 0)
			return result;

		while (count > 0) {
			double rand = Math.random();
			int baseSeed = (int) (rand * (conditionCount - 1));
			if (!matchs.get(baseSeed)) {
				matchs.set(baseSeed, true);
				unmatchedCount--;
			} else if (count <= unmatchedCount) {
				condis.remove(baseSeed);
				matchs.remove(baseSeed);
				conditionCount = condis.size();
				continue;
			}
			result += condis.get(baseSeed).charAt(
					(int) (rand * (condis.get(baseSeed).length() - 1)));
			count--;
		}
		return result;
	}

	public static long hex_10(String hex) {
		return Long.parseLong(hex, 16);
	}

	/**
	 * 生成安全码 时间：14000 00000 000 随机数： 88888 8 混合后： 1848080808 080000 000 转16进制：
	 * 
	 * @return
	 */
	public static Random random = new Random();

	public static String generateUUIDHex16() {
		char[] times = String.valueOf(System.currentTimeMillis()).toCharArray();
		char[] rands = String.valueOf(random.nextFloat()).substring(2)
				.toCharArray();
		StringBuilder sb = new StringBuilder();
		for (int i = 0; i < times.length; i++) {
			if (i < rands.length && i < 6)
				sb.append(rands[i]);
			sb.append(times[i]);
		}
		return Long.toHexString(Long.parseLong(sb.toString()));
	}

	public static void main(String[] args) {
		System.out.println(generateUUID());
	}
}
